import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:oramemo/core/repositories/default_api.dart';
import 'package:oramemo/entity/system_language_entity.dart';

import 'language_title.dart';

class LanguageController extends GetxController with BaseControllerMixin {
  @override
  String get builderId => 'language';

  final languages = [
    // LanguageTitle('简体中文', 'zh', 'zh', 'CN'),
    // LanguageTitle('English', 'en', 'en', 'US'),
  ];

  LanguageController();

  changeLang(LanguageTitle language) {
    changeLanguage(Locale(language.languageCode, language.countryCode));
  }

  @override
  void onInit() async {
    await onLoad();
    super.onInit();
  }

  Future<void> onLoad() async {
    SystemLanguageEntity? systemLanguageEntity = await DefaultApi()
        .systemDictDataTypeDictTypeGet<SystemLanguageEntity>(
            dictType: 'biz_system_language');
    if (systemLanguageEntity?.data?.isNotEmpty == true) {
      languages.clear();
      systemLanguageEntity?.data?.forEach((element) {
        languages.add(LanguageTitle(
            element.dictLabel ?? '',
            element.dictValue ?? '',
            element.dictValue ?? '',
            element.dictValue ?? ''));
      });
    }
    update();
  }
}
